#include <stdio.h>
#include <stdlib.h>
#include <time.h>
          
int main(int argc, char* argv[]){

    FILE *in , *out;

    char key[128];
    int i,j,n,m,max;

	clock_t execution_time;

	char *input = argv[1];
	char *output = argv[2];
    
	int **red, **green, **blue;
	unsigned char temp1, temp2, temp3;

    in = fopen(input, "r");
    if(in == NULL) {
    	printf("Erro na abertura do arquivo in.ppm \n");
    	return 0 ;
    }

	out = fopen(output, "w") ;
    if(out == NULL) {
    	printf("Erro na abertura do arquivo out.ppm\n");
    	return 0 ;
    }

    fscanf(in, "%s", key);
	fscanf(in, "%d %d %d", &m, &n, &max);
	
    if(!strcmp(key,"P3")){
    	red = (int**)malloc(n*sizeof(int*));
	    for(i=0; i<n; i++)
    	    red[i] = (int*)malloc(m*sizeof(int));
		green = (int**)malloc(n*sizeof(int*));
		for(i=0; i<n; i++)
    	    green[i] = (int*)malloc(m*sizeof(int));
		blue = (int**)malloc(n*sizeof(int*));
	    for(i=0; i<n; i++)
	        blue[i] = (int*)malloc(m*sizeof(int));
		// Leitura da imagem
    	for(i=0; i<=n-1; i++)
        	for(j=0;j<=m-1;j++)
            	fscanf(in, " %d %d %d ", &red[i][j] , &green[i][j] , &blue[i][j]);
    }
	else if(!strcmp(key, "P6")){
	    red = (int**)malloc(n*sizeof(int*));
	    for(i=0; i<n; i++)
	        red[i] = (int*)malloc(m*sizeof(int));
	    green = (int**)malloc(n*sizeof(int*));
	    for(i=0; i<n; i++)
	        green[i] = (int*)malloc(m*sizeof(int));
	    blue = (int**)malloc(n*sizeof(int*));
	    for(i=0; i<n; i++)
	        blue[i] = (int*)malloc(m*sizeof(int));

	    // Leitura da imagem
	    for(i=0; i<=n-1; i++){
	        for(j=0;j<=m-1;j++){
	            fread(&temp1, 1, 1, in);
	            fread(&temp2, 1, 1, in);
	            fread(&temp3, 1, 1, in);
				red[i][j] = (int)temp1;
				green[i][j] = (int)temp2;
				blue[i][j] = (int)temp3;
			}
		}
	}
	else{
    	printf("Arquivo nao e um PPM\n");
    	fclose(in);
    	return -1;
	}

	execution_time = clock();
	//percorrendo toda a imagem, exceto a borda
    for(i=2; i<=n-3; i++)
        for(j=2;j<=m-3;j++){

                red[i][j] = (red[i-2][j-2] + red[i-1][j-2] + red[i][j-2] + red[i+1][j-2] + red[i+2][j-2]
	  + red[i-2][j-1] + red[i-1][j-1] + red[i][j-1] + red[i+1][j-1] + red[i+2][j-1]
	  + red[i-2][j] + red[i-1][j] + red[i][j] + red[i+1][j] + red[i+2][j]
  	  + red[i-2][j+1] + red[i-1][j+1] + red[i][j+1] + red[i+1][j+1] + red[i+2][j+1]
 	  + red[i-2][j+2] + red[i-1][j+2] + red[i][j+2] + red[i+1][j+2] + red[i+2][j+2])/25;

                green[i][j] = (green[i-2][j-2] + green[i-1][j-2] + green[i][j-2] + green[i+1][j-2] + green[i+2][j-2]
	  + green[i-2][j-1] + green[i-1][j-1] + green[i][j-1] + green[i+1][j-1] + green[i+2][j-1]
	  + green[i-2][j] + green[i-1][j] + green[i][j] + green[i+1][j] + green[i+2][j]
  	  + green[i-2][j+1] + green[i-1][j+1] + green[i][j+1] + green[i+1][j+1] + green[i+2][j+1]
 	  + green[i-2][j+2] + green[i-1][j+2] + green[i][j+2] + green[i+1][j+2] + green[i+2][j+2])/25;

		blue[i][j] = (blue[i-2][j-2] + blue[i-1][j-2] + blue[i][j-2] + blue[i+1][j-2] + blue[i+2][j-2]
	  + blue[i-2][j-1] + blue[i-1][j-1] + blue[i][j-1] + blue[i+1][j-1] + blue[i+2][j-1]
	  + blue[i-2][j] + blue[i-1][j] + blue[i][j] + blue[i+1][j] + blue[i+2][j]
  	  + blue[i-2][j+1] + blue[i-1][j+1] + blue[i][j+1] + blue[i+1][j+1] + blue[i+2][j+1]
 	  + blue[i-2][j+2] + blue[i-1][j+2] + blue[i][j+2] + blue[i+1][j+2] + blue[i+2][j+2])/25;
    }

    //calculando o tempo
   	execution_time = clock() - execution_time;
	printf("\n%.6f\n",((float)execution_time)/CLOCKS_PER_SEC);

    // Forma o arquivo de saída

    fprintf(out,"P3\n" );
    fprintf(out, "%d %d\n%d", m, n, max);
          
    for(i=0; i<=n-1; i++)
        for(j=0;j<=m-1;j++)
            fprintf(out, "\n%d\n%d\n%d ",red[i][j],green[i][j],blue[i][j]);

    fclose(in);
    fclose(out);

    return 0;
}
